//index.js
//获取应用实例
var app = getApp()
let api = require("../../api/api.js")

Page({
  data: {
    goodsList: [],
    totalPrice: 0,
    singer_mobile: "",
    post_script:"",
    order_mount: 0,
  },
  onShow: function () {
    
  },

  onLoad: function (e) {
    console.log(e)
    let carts = wx.getStorageSync('carts')
    let order_mount = 0;
    this.setData({ goodsList: carts })
    this.setData({ totalPrice: e.totalPrice })
    console.log(this.data.goodsList)
    for(let i = 0; i < this.data.goodsList.length; i++){
      order_mount += this.data.goodsList[i].course.shop_price
    }
    this.setData({ order_mount: order_mount })
  },


  createOrder: function (e) {
    // 手机号正则表达式
    const mobilePhone$ = /^(13[0-9]|14[5-9]|15[012356789]|166|17[0-8]|18[0-9]|19[8-9])[0-9]{8}$/;
    if (this.data.singer_mobile == '') {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'none',
        duration: 2000
      })
      return;
    } else if (!mobilePhone$.test(this.data.singer_mobile)) {
      wx.showToast({
        title: '手机号格式不正确',
        icon: 'none',
        duration: 2000
      })
      return;
    }

    //提交请求调用创建订单
    api.createOrder(this.data.singer_mobile, this.data.post_script, this.data.order_mount).then(res=>{
      console.log(res)
      api.startPay(res.data)
      wx.hideLoading()
    })

  },

  getMobile:function(e){
    this.setData({
      singer_mobile: e.detail.value
    })
  },

  getPostScript: function(e){
    this.setData({
      post_script: e.detail.value
    })
  }
  
})